#!/usr/local/bin/python
#-*- encoding:utf-8 -*- 
import string 
from whoosh.index import create_in  
from whoosh.fields import *  
import jieba.analyse
from whoosh.index import open_dir
from whoosh.scoring import *
from whoosh.matching import *
from whoosh.qparser import QueryParser
from whoosh.index import open_dir
analyzer = jieba.analyse.ChineseAnalyzer();
import utils

config = utils.get_config(); 
labeled = open('LabeledData/label_week.txt', 'r');
ix = open_dir(config.get('bm25', 'BackupIndex'));
searcher = ix.searcher();
reader = ix.reader();
queryID = 0;
labeled_format = open('labed_week_format.txt', 'w')
for line in labeled:
    queryID += 1;
    li = line.strip().split('|');
    queryString = li[0];
    for i in range(1, len(li)):
        id, appID, score = li[i].split(':');
        docnum = searcher.document_number(appid=appID);
        if docnum:
            fields=reader.stored_fields(docnum);
            content = fields['content'];
            description = fields['description'];
            labeled_format.write("%d,%s,%s,%d,%s,%s,%s\n" %(queryID, queryString, appID, 0, score, content.strip().encode('utf-8'), description.encode('utf-8')));
        else:
            content = 'WRONG';
            description = 'WRONG';    

labeled.close();
labeled_format.close();
